package com.zhanghe.wzq.handler.request;

import com.zhanghe.wzq.Table;
import com.zhanghe.wzq.User;
import com.zhanghe.wzq.command.request.CreateTableRequest;
import com.zhanghe.wzq.command.response.CreateTableResponse;
import com.zhanghe.wzq.data.Attributes;
import com.zhanghe.wzq.data.TableManager;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * CreateTableRequestHandler
 *
 * @author Clevo
 * @date 2018/12/25
 */
public class CreateTableRequestHandler extends SimpleChannelInboundHandler<CreateTableRequest> {

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, CreateTableRequest createTableRequest) throws Exception {
        User user = channelHandlerContext.channel().attr(Attributes.USER).get();
        CreateTableResponse createTableResponse = new CreateTableResponse();
        if(user != null){
            Table table = TableManager.INSTANCE.createNewTable(user.getId());
            createTableResponse.setSuccess(true);
            createTableResponse.setTableId(table.getId());
        }else{
            createTableResponse.setSuccess(false);
            createTableResponse.setMsg("未登录!");
        }
        channelHandlerContext.channel().writeAndFlush(createTableResponse);
    }
}
